package com.vegetable.modules.service.baseData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.baseData.UnitPackagePriceRecord;
import com.vegetable.modules.mapper.baseData.UnitPackagePriceRecordMapper;
import com.vegetable.modules.service.baseData.IUnitPackagePriceRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
public class UnitPackagePriceRecordServiceImpl extends ServiceImpl<UnitPackagePriceRecordMapper, UnitPackagePriceRecord> implements IUnitPackagePriceRecordService {

    @Override
    public BigDecimal queryUnitPackagePrice(Long commodityId, Long specsId, Long customerId) {
        BigDecimal num = BigDecimal.ZERO;
        QueryWrapper<UnitPackagePriceRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("commodity_id", commodityId);
        wrapper.eq("customer_id", customerId);
        if(ObjectUtil.isNull(specsId)){
            wrapper.isNull("specs_id");
        }else {
            wrapper.eq("specs_id", specsId);
        }
        UnitPackagePriceRecord record = this.baseMapper.selectOne(wrapper);
        if(ObjectUtil.isNotNull(record)){
            num = record.getPackageUnit();
        }
        return num;
    }

    @Override
    public void recordUnitPackagePrice(Long commodityId, Long specsId, BigDecimal packageUnitPrice, Long customerId) {
        UnitPackagePriceRecord record = new UnitPackagePriceRecord();
        record.setCommodityId(commodityId);
        record.setSpecsId(specsId);
        record.setPackageUnit(packageUnitPrice);
        record.setCustomerId(customerId);
        QueryWrapper<UnitPackagePriceRecord> wrapper = new QueryWrapper();
        wrapper.eq("commodity_id", commodityId);
        wrapper.eq("customer_id", customerId);
        if(ObjectUtil.isNull(specsId)){
            wrapper.isNull("specs_id");
        }else {
            wrapper.eq("specs_id", specsId);
        }
        this.saveOrUpdate(record, wrapper);
    }
}
